home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / FileInputStream.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  9.0 KB  |  254 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)FileInputStream.java    1.42 98/09/24
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * A <code>FileInputStream</code> obtains input bytes
  19.  * from a file in a file system. What files
  20.  * are  available depends on the host environment.
  21.  *
  22.  * @author  Arthur van Hoff
  23.  * @version 1.42, 09/24/98
  24.  * @see     java.io.File
  25.  * @see     java.io.FileDescriptor
  26.  * @see        java.io.FileOutputStream
  27.  * @since   JDK1.0
  28.  */
  29. public
  30. class FileInputStream extends InputStream
  31. {
  32.     /* File Descriptor - handle to the open file */
  33.     private FileDescriptor fd;
  34.  
  35.     /**
  36.      * Creates a <code>FileInputStream</code> by
  37.      * opening a connection to an actual file,
  38.      * the file named by the path name <code>name</code>
  39.      * in the file system.  A new <code>FileDescriptor</code>
  40.      * object is created to represent this file
  41.      * connection.
  42.      * <p>
  43.      * First, if there is a security
  44.      * manager, its <code>checkRead</code> method
  45.      * is called with the <code>name</code> argument
  46.      * as its argument.
  47.      * <p>
  48.      * If the named file does not exist, is a directory rather than a regular
  49.      * file, or for some other reason cannot be opened for reading then a
  50.      * <code>FileNotFoundException</code> is thrown.
  51.      *
  52.      * @param      name   the system-dependent file name.
  53.      * @exception  FileNotFoundException  if the file does not exist,
  54.      *                   is a directory rather than a regular file,
  55.      *                   or for some other reason cannot be opened for
  56.      *                   reading.
  57.      * @exception  SecurityException      if a security manager exists and its
  58.      *               <code>checkRead</code> method denies read access
  59.      *               to the file.
  60.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  61.      */
  62.     public FileInputStream(String name) throws FileNotFoundException {
  63.     SecurityManager security = System.getSecurityManager();
  64.     if (security != null) {
  65.         security.checkRead(name);
  66.     }
  67.     fd = new FileDescriptor();
  68.     open(name);
  69.     }
  70.  
  71.     /**
  72.      * Creates a <code>FileInputStream</code> by
  73.      * opening a connection to an actual file,
  74.      * the file named by the <code>File</code>
  75.      * object <code>file</code> in the file system.
  76.      * A new <code>FileDescriptor</code> object
  77.      * is created to represent this file connection.
  78.      * <p>
  79.      * First, if there is a security manager,
  80.      * its <code>checkRead</code> method  is called
  81.      * with the path represented by the <code>file</code>
  82.      * argument as its argument.
  83.      * <p>
  84.      * If the named file does not exist, is a directory rather than a regular
  85.      * file, or for some other reason cannot be opened for reading then a
  86.      * <code>FileNotFoundException</code> is thrown.
  87.      *
  88.      * @param      file   the file to be opened for reading.
  89.      * @exception  FileNotFoundException  if the file does not exist,
  90.      *                   is a directory rather than a regular file,
  91.      *                   or for some other reason cannot be opened for
  92.      *                   reading.
  93.      * @exception  SecurityException      if a security manager exists and its
  94.      *               <code>checkRead</code> method denies read access to the file.
  95.      * @see        java.io.File#getPath()
  96.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  97.      */
  98.     public FileInputStream(File file) throws FileNotFoundException {
  99.     this(file.getPath());
  100.     }
  101.  
  102.     /**
  103.      * Creates a <code>FileInputStream</code> by
  104.      * using the file  descriptor <code>fdObj</code>,
  105.      * which represents an existing connection
  106.      * to an actual file in the  file system.
  107.      * <p>
  108.      * First, if there is a security manager, its
  109.      * <code>checkRead</code> method  is called
  110.      * with the file descriptor <code>fdObj</code>
  111.      * as its argument to see if it's ok to read the file descriptor.
  112.      *
  113.      * @param      fdObj   the file descriptor to be opened for reading.
  114.      * @throws  SecurityException
  115.      *          if a security manager exists and its <code>checkRead</code> method denies
  116.      *          read access to the file descriptor.
  117.      * @see        SecurityManager#checkRead(java.io.FileDescriptor)
  118.      */
  119.     public FileInputStream(FileDescriptor fdObj) {
  120.     SecurityManager security = System.getSecurityManager();
  121.     if (fdObj == null) {
  122.         throw new NullPointerException();
  123.     }
  124.     if (security != null) {
  125.         security.checkRead(fdObj);
  126.     }
  127.     fd = fdObj;
  128.     }
  129.  
  130.     /**
  131.      * Opens the specified file for reading.
  132.      * @param name the name of the file
  133.      */
  134.     private native void open(String name) throws FileNotFoundException;
  135.  
  136.     /**
  137.      * Reads a byte of data from this input stream. This method blocks
  138.      * if no input is yet available.
  139.      *
  140.      * @return     the next byte of data, or <code>-1</code> if the end of the
  141.      *             file is reached.
  142.      * @exception  IOException  if an I/O error occurs.
  143.      */
  144.     public native int read() throws IOException;
  145.  
  146.  
  147.     /**
  148.      * Reads a subarray as a sequence of bytes.
  149.      * @param b the data to be written
  150.      * @param off the start offset in the data
  151.      * @param len the number of bytes that are written
  152.      * @exception IOException If an I/O error has occurred.
  153.      */
  154.     private native int readBytes(byte b[], int off, int len) throws IOException;
  155.  
  156.     /**
  157.      * Reads up to <code>b.length</code> bytes of data from this input
  158.      * stream into an array of bytes. This method blocks until some input
  159.      * is available.
  160.      *
  161.      * @param      b   the buffer into which the data is read.
  162.      * @return     the total number of bytes read into the buffer, or
  163.      *             <code>-1</code> if there is no more data because the end of
  164.      *             the file has been reached.
  165.      * @exception  IOException  if an I/O error occurs.
  166.      */
  167.     public int read(byte b[]) throws IOException {
  168.     return readBytes(b, 0, b.length);
  169.     }
  170.  
  171.     /**
  172.      * Reads up to <code>len</code> bytes of data from this input stream
  173.      * into an array of bytes. This method blocks until some input is
  174.      * available.
  175.      *
  176.      * @param      b     the buffer into which the data is read.
  177.      * @param      off   the start offset of the data.
  178.      * @param      len   the maximum number of bytes read.
  179.      * @return     the total number of bytes read into the buffer, or
  180.      *             <code>-1</code> if there is no more data because the end of
  181.      *             the file has been reached.
  182.      * @exception  IOException  if an I/O error occurs.
  183.      */
  184.     public int read(byte b[], int off, int len) throws IOException {
  185.     return readBytes(b, off, len);
  186.     }
  187.  
  188.     /**
  189.      * Skips over and discards <code>n</code> bytes of data from the
  190.      * input stream. The <code>skip</code> method may, for a variety of
  191.      * reasons, end up skipping over some smaller number of bytes,
  192.      * possibly <code>0</code>. The actual number of bytes skipped is returned.
  193.      *
  194.      * @param      n   the number of bytes to be skipped.
  195.      * @return     the actual number of bytes skipped.
  196.      * @exception  IOException  if an I/O error occurs.
  197.      */
  198.     public native long skip(long n) throws IOException;
  199.  
  200.     /**
  201.      * Returns the number of bytes that can be read from this file input
  202.      * stream without blocking.
  203.      *
  204.      * @return     the number of bytes that can be read from this file input
  205.      *             stream without blocking.
  206.      * @exception  IOException  if an I/O error occurs.
  207.      */
  208.     public native int available() throws IOException;
  209.  
  210.     /**
  211.      * Closes this file input stream and releases any system resources
  212.      * associated with the stream.
  213.      *
  214.      * @exception  IOException  if an I/O error occurs.
  215.      */
  216.     public native void close() throws IOException;
  217.  
  218.     /**
  219.      * Returns the <code>FileDescriptor</code>
  220.      * object  that represents the connection to
  221.      * the actual file in the file system being
  222.      * used by this <code>FileInputStream</code>.
  223.      *
  224.      * @return     the file descriptor object associated with this stream.
  225.      * @exception  IOException  if an I/O error occurs.
  226.      * @see        java.io.FileDescriptor
  227.      */
  228.     public final FileDescriptor getFD() throws IOException {
  229.     if (fd != null) return fd;
  230.     throw new IOException();
  231.     }
  232.  
  233.     private static native void initIDs();
  234.  
  235.     static {
  236.     initIDs();
  237.     }
  238.  
  239.     /**
  240.      * Ensures that the <code>close</code> method of this file input stream is
  241.      * called when there are no more references to it.
  242.      *
  243.      * @exception  IOException  if an I/O error occurs.
  244.      * @see        java.io.FileInputStream#close()
  245.      */
  246.     protected void finalize() throws IOException {
  247.     if (fd != null) {
  248.         if (fd != fd.in) {
  249.         close();
  250.         }
  251.     }
  252.     }
  253. }
  254.